package org.dreamfly.core.service.impl;

import imageretrievalcopy.ImageContentSearch;
import org.apache.struts2.ServletActionContext;
import org.dreamfly.core.action.UploadFile;
import org.dreamfly.core.bean.SzPictureType;
import org.dreamfly.core.dao.PictureTypeDAO;
import org.dreamfly.core.service.PictureTypeService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-9-18
 * Time: 10:46:04
 * To change this template use File | Settings | File Templates.
 */
public class PictureTypeServiceImpl implements PictureTypeService {
    private PictureTypeDAO pictureTypeDAO;
    @Override
    public void saveOrUpdate(SzPictureType pictureType) {
    	pictureTypeDAO.saveOrUpdate(pictureType);
    }

    @Override
    public SzPictureType addIndexURl(SzPictureType pictureType) {
        /*try {
            pictureType.setName(new String(pictureType.getName().getBytes("ISO-8859-1"),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }*/
        ImageContentSearch contentSearch = new ImageContentSearch();
        String root = ServletActionContext.getServletContext().getRealPath(UploadFile.uploadFolder +"/picture");
        String name = new String(pictureType.getName());
        pictureType.setName(name);
        contentSearch.createClass(root,name,"image","index");
        pictureType.setClassUrl(root+"\\"+name);
        pictureType.setClassImageUrl(pictureType.getClassUrl()+"\\image");
        pictureType.setClassIndexUrl(pictureType.getClassUrl()+"\\index");
        return pictureType;
    }

    @Override
    public SzPictureType getUnique(Long id) {
        return pictureTypeDAO.find(SzPictureType.class,id);
    }

    public void setPictureTypeDAO(PictureTypeDAO pictureTypeDAO) {
        this.pictureTypeDAO = pictureTypeDAO;
    }

	/* (non-Javadoc)
	 * @see org.dreamfly.core.service.PictureTypeService#getAll()
	 */
	@Override
	public List<SzPictureType> getAll() {
		return pictureTypeDAO.findAll(SzPictureType.class);
	}

    @Override
    public void deleteType(Long id) {
        SzPictureType pictureType = pictureTypeDAO.find(SzPictureType.class, id);
        ImageContentSearch contentSearch = new ImageContentSearch();
        contentSearch.delClass(pictureType.getClassUrl());
        pictureTypeDAO.delete(pictureType);
    }

    @Override
    public List<SzPictureType> getAllByServer(String serverName) {
        return pictureTypeDAO.findAllByServer(serverName);
    }

    @Override
    public void modify(SzPictureType pictureType) {
        pictureTypeDAO.modifyName(pictureType);
    }
}
